package com.zzq.my.jetpacks.jectpackapp

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.lifecycle.Lifecycle
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.zzq.my.jetpacks.jectpackapp.ui.fragment.*

/**
 * 描述：class describe
 * 作者：zhuangzeqin
 * 时间: 2021/12/24-16:59
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
/* ------注释说明--Fragment 常量key------ */
const val FRAGMENT_ONE = "fragment_one"
const val FRAGMENT_TWO = "fragment_two"
const val FRAGMENT_THREE = "fragment_three"



const val FRAGMENT_A = "fragment_a"
const val FRAGMENT_B = "fragment_b"
const val FRAGMENT_C = "fragment_c"

/**
 * 获取OneFragment、TwoFragment、ThreeFragment,该集合中的fragment不包含嵌套。
 */
fun generate123Fragments() = mapOf<String, Fragment>(
    FRAGMENT_ONE to OneFragment.newInstance(FRAGMENT_ONE),
    FRAGMENT_TWO to TwoFragment.newInstance(FRAGMENT_TWO),
    FRAGMENT_THREE to ThreeFragment.newInStance(FRAGMENT_THREE)
)
/**
 * 获取OneFragment、TwoFragment、ThreeFragment的Title
 */
fun generate123FragmentTitles()= mutableListOf<String>().apply {
    add(FRAGMENT_ONE)
    add(FRAGMENT_TWO)
    add(FRAGMENT_THREE)
}

/**
 * 获取AFragment、BFragment、CFragment
 */
fun generateABCFragments() = mapOf<String, Fragment>(
    FRAGMENT_A to AFragment.newInstance(),
    FRAGMENT_B to BFragment.newInstance(),
    FRAGMENT_C to CFragment.newInStance()
)
/**
 * 获取AFragment、BFragment、CFragment的Title
 */
fun generateABCFragmentTitles() = mutableListOf<String>().apply {
    add(FRAGMENT_A)
    add(FRAGMENT_B)
    add(FRAGMENT_C)
}

/**
 * 构建多个TextFragment
 */
fun generateTextFragments(size: Int) = mutableListOf<Fragment>().apply {
    for (i in 0..size) {
        add(TextFragment.newInstance("Fragment:${i + 1}"))
    }

}

/**
 * 构建多个TextFragment的Title
 */
fun generateTextFragmentTitles(amount: Int) = mutableListOf<String>().apply {
    for (i in 0..amount) {
        add("Fragment:${i+1}")
    }
}
/**
 * 给ViewPager绑定Fragment
 *  mBinding.viewPager.bindFragment(
supportFragmentManager,
listOf(LazyLoad1Fragment.obtainFragment(), LazyLoad2Fragment.obtainFragment(), LazyLoad3Fragment.obtainFragment()),
listOf("Demo1", "Demo2", "Demo3"),
behavior = 1
)
 */
fun ViewPager.bindFragment(
    fm: FragmentManager,
    fragments: List<Fragment>,
    pageTitles: List<String>? = null,
    behavior: Int = 0
): ViewPager {
    offscreenPageLimit = fragments.size - 1
    adapter = object : FragmentStatePagerAdapter(fm, behavior) {
        override fun getItem(p: Int) = fragments[p]
        override fun getCount() = fragments.size
        override fun getPageTitle(p: Int) = if (pageTitles == null) null else pageTitles[p]
    }
    return this
}

/**
 * 给ViewPager2绑定Fragment
 *  mBinding.viewPager2.bindFragment(
supportFragmentManager,
this.lifecycle,
listOf(LazyLoad1Fragment.obtainFragment(), LazyLoad2Fragment.obtainFragment(), LazyLoad3Fragment.obtainFragment())
)

val title = listOf("Demo1", "Demo2", "Demo3")
TabLayoutMediator(mBinding.tabLayout, mBinding.viewPager2) { tab, position ->
//回调
tab.text = title[position]
}.attach()

 */
fun ViewPager2.bindFragment(
    fm: FragmentManager,
    lifecycle: Lifecycle,
    fragments: List<Fragment>
): ViewPager2 {
    offscreenPageLimit = fragments.size - 1

    adapter = object : FragmentStateAdapter(fm, lifecycle) {
        override fun getItemCount(): Int = fragments.size
        override fun createFragment(position: Int): Fragment = fragments[position]
    }
    return this
}




